package com.nuanshui.heatedloan.order.integration.srping;

import com.nuanshui.framework.lang.ClassUtil;
import com.nuanshui.framework.springContext.SpringContextUtil;
import com.nuanshui.heatedloan.order.enums.OrderOptionType;
import com.nuanshui.heatedloan.order.integration.annotation.OrderExecuteAnnotation;
import com.nuanshui.heatedloan.order.integration.bean.OrderHandlerMeta;
import com.nuanshui.heatedloan.order.integration.execute.OrderExecute;
import com.nuanshui.heatedloan.order.integration.execute.OrderExecuteFacade;
import com.nuanshui.heatedloan.order.integration.execute.OrderExecuteFacadeImpl;
import com.nuanshui.heatedloan.order.integration.execute.bizexecute.AbstractOrderExecute;
import com.nuanshui.heatedloan.order.integration.execute.handler.OrderHandler;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Set;

@Component
public class OrderExecuteFactoryBean implements BeanFactoryAware, BeanPostProcessor, FactoryBean<OrderExecuteFacade> {

	private BeanFactory beanFactory = null;
	
	private OrderExecuteFacadeImpl orderExecuteFacade = new OrderExecuteFacadeImpl();

	private volatile boolean isInitEd = false;

	@Override
	public OrderExecuteFacade getObject() throws Exception {
		return orderExecuteFacade;
	}
	
	@Override
	public Class<?> getObjectType() {
		return OrderExecuteFacade.class;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		Class<?> targetClass = AopUtils.getTargetClass(bean);
		OrderExecuteAnnotation orderExecuteAnnotation = AnnotationUtils.getAnnotation(targetClass, OrderExecuteAnnotation.class);

		if (orderExecuteAnnotation != null && bean instanceof OrderHandler) {
			if (!isInitEd) {
				synchronized (orderExecuteAnnotation) {
					if (!isInitEd) {
						isInitEd = true;
						Set<Class<?>> OrderHandlers = ClassUtil.getAnnotationClasses(OrderExecuteAnnotation.class, "com.nuanshui.heatedloan.order.integration.execute.handler.syshandler");
						for (Class<?> orderHandlerClass : OrderHandlers) {
							OrderExecuteAnnotation orderExecuteAnnotationTemp = orderHandlerClass.getAnnotation(OrderExecuteAnnotation.class);
							if (orderExecuteAnnotationTemp != null) {
								OrderHandlerMeta meta = new OrderHandlerMeta();
								meta.setOrder(orderExecuteAnnotationTemp.order());
								meta.setOrderHandler((OrderHandler) SpringContextUtil.getBean(orderHandlerClass));
								meta.setNotSupportOrderType(CollectionUtils.arrayToList(orderExecuteAnnotationTemp.notSupportOrderType()));
								meta.setSupportOrderType(CollectionUtils.arrayToList(orderExecuteAnnotationTemp.supportOrderType()));
								orderExecuteFacade.getOrderExecuteRegister().regisgerOrderHandlers(meta);
							}
						}
					}
				}
			}

			if (bean instanceof AbstractOrderExecute) { //表示是最后执行节点
				OrderOptionType type = orderExecuteAnnotation.bizOrderOptionType();
				orderExecuteFacade.getOrderExecuteRegister().registerFinalOrderHandler(type, bean);//注册一个最终的处理器handler
				orderExecuteFacade.getOrderExecuteRegister().registerExecute(type, (OrderExecute)bean); //注册一个执行期execute
			}

		}
		return bean;
	}

	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		this.beanFactory = beanFactory;
	}

}
